package lc.双指针;

/**
 * 长度最小的子数组
 *
 * @author yang
 * @date 2021/12/26
 */
public class 长度最小的子数组 {

    public int minSubArrayLen(int target, int[] nums) {

        int sum = 0;
        int min = Integer.MAX_VALUE;
        int i = 0;
        int subLen = 0;
        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            while (sum >= target) {
                subLen = j - i + 1;
                min = min > subLen ? subLen : min;
                sum-=nums[i++];
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }
}
